package com.cn.springcloudwechat.serviceImpl.order;

import com.cn.springcloudwechat.common.mybatis.Page;
import com.cn.springcloudwechat.common.utils.OrderUtil;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.mapper.mapper.find.courseall.YmCoursePackageMapper;
import com.cn.springcloudwechat.mapper.mapper.order.YmOrderGoodsMapper;
import com.cn.springcloudwechat.mapper.mapper.order.YmOrderMapper;
import com.cn.springcloudwechat.model.YmCoursePackage;
import com.cn.springcloudwechat.model.order.YmOrder;
import com.cn.springcloudwechat.model.order.YmOrderGoods;
import com.cn.springcloudwechat.service.order.YmOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: Administrator
 * @create: 2018/7/31 19:00
 */
@Service
public class YmOrderServiceImpl implements YmOrderService {

    @Autowired
    YmOrderGoodsMapper ymOrderGoodsMapper;
    @Autowired
    YmOrderMapper ymOrderMapper;

    @Autowired
    YmCoursePackageMapper coursePackageMapper;

    @Autowired
    YmCoursePackageMapper ymCoursePackageMapper;

    @Override
    public Page<Object> getAllYmOrder(YmOrder record, Page<Object> page) {
        List<Object> ymOrders = ymOrderMapper.getAllYmOrder(record, page);
        page.setResults(ymOrders);
        return page;
    }

    @Override
    public Page<Object> getAllYmOrderGoods(YmOrderGoods record, Page<Object> page) {
        page.setResults(ymOrderGoodsMapper.getAllYmOrderGoods(record, page));
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insertYmOrderGoodsAndYmOrder(YmOrder ymOrder, YmOrderGoods ymOrderGoods) {
        String outTradeNo = OrderUtil.getOutTradeNo();
        ymOrder.setOutTradeNo(outTradeNo);
        //查询课程包详情数据并获取金额
        YmCoursePackage ymCoursePackage = coursePackageMapper.getCoursePageDataByCoursePageId(ymOrderGoods.getGoodsId());
         Long price=0L;
        if ( ymCoursePackage.getDiscountPrice() == null ||  ymCoursePackage.getDiscountPrice() == 0) {
            price = ymCoursePackage.getTotalPrice();
        }else{
            price = ymCoursePackage.getDiscountPrice();
        }
        ymOrderGoods.setGoodsPrice(price);
        ymOrder.setProductCost(price);
        ymOrder.setTotalCost(price);
        ymOrder.setUserId(ThreadLocalUtil.getUser().getUserId());
        ymOrderMapper.insertSelective(ymOrder);
        //设置订单号
        ymOrderGoods.setOrderId(ymOrder.getOrderId());
        ThreadLocalUtil.getRequest().getSession().setAttribute("outTradeNo", outTradeNo);
        String a = (String) ThreadLocalUtil.getRequest().getSession().getAttribute("outTradeNo");
        ymOrderGoodsMapper.insertSelective(ymOrderGoods);
        return ymOrder.getOrderId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateYmOrderGoods(YmOrderGoods ymOrderGoods) {
        ymOrderGoodsMapper.updateByPrimaryKeySelective(ymOrderGoods);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateYmOrder(YmOrder ymOrder) {
        Integer result = ymOrderMapper.updateByPrimaryKeySelective(ymOrder);
        if(result != 0){
            List<YmOrderGoods> ymOrderGoodsList = ymOrderGoodsMapper.getOrderGoodsByOrderId(ymOrder.getOrderId());
            for(YmOrderGoods ymOrderGoods:ymOrderGoodsList){
                if(ymOrderGoods.getGoodsType()==6){
                    YmCoursePackage ymCoursePackage = ymCoursePackageMapper.getYmCoursePackageByOrderId(ymOrder.getOrderId());
                    Integer total = 0;
                    if(ymCoursePackage.getSaleTotalNumber() != null){
                        total=ymCoursePackage.getSaleTotalNumber();
                    }
                    ymCoursePackage.setSaleTotalNumber(total+1);
                    ymCoursePackageMapper.updateByPrimaryKeySelective(ymCoursePackage);
                }
            }
        }
    }

    @Override
    @Cacheable
    public YmOrder getYmOrder(long orderId) {
        return ymOrderMapper.selectByPrimaryKey(orderId);
    }

    @Override
    @Cacheable
    public YmOrderGoods getYmOrderGoods(long goodsIdDesc) {
        return ymOrderGoodsMapper.selectByPrimaryKey(goodsIdDesc);
    }

    @Override
    public List<YmOrderGoods> getOrderGoodsByOrderId(long orderId) {
        return ymOrderGoodsMapper.getOrderGoodsByOrderId(orderId);
    }

    @Override
    public YmOrder getOrderPayStateByOutTradeNo(String outTradeNo) {
        return ymOrderMapper.getOrderPayStateByOutTradeNo(outTradeNo);
    }
}
